package me.znext.learn.android;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import me.znext.learn.android.db.UserDAO;

import java.util.HashMap;

public class SignupActivity extends AppCompatActivity {
  private Storage storage = Storage.getStorage();
  private final Context context = this;
  private EditText unameEditText;
  private EditText pwEditText;
  private EditText pwRepeatEditText;
  private UserDAO userDAO;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    this.setTitle("注册");

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    unameEditText = (EditText) findViewById(R.id.unameEditText);
    pwEditText = (EditText) findViewById(R.id.pwEditText);
    pwRepeatEditText = (EditText) findViewById(R.id.pwRepeatEditText);

    Button signUpBtn = (Button) findViewById(R.id.signUpBtn);
    assert signUpBtn != null;
    signUpBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        signUp(String.valueOf(unameEditText.getText()), String.valueOf(pwEditText.getText()), String.valueOf(pwRepeatEditText.getText()));
      }
    });

    Button signInBtn = (Button) findViewById(R.id.signInBtn);
    assert signInBtn != null;
    signInBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent intent = new Intent();
        intent.setClass(SignupActivity.this, SignInActivity.class);
        startActivity(intent);
        // 停止当前的Activity,如果不写,则按返回键会跳转回原来的Activity
        finish();
      }
    });

    userDAO = new UserDAO(this);
  }

  @SuppressWarnings("Duplicates")
  private void signUp(String uname, String pw, String pwRepeat) {
    uname = uname.trim();
    if (!uname.isEmpty()) {
      pw = pw.trim();
      if (!pw.isEmpty()) {
        System.out.print(pw);
        System.out.print(pwRepeat);
        if (pw.equals(pwRepeat)) {
          Intent intent = new Intent();
          intent.setClass(SignupActivity.this, CourseSchedule.class);
          HashMap<String, String> profile = new HashMap<>();
          profile.put("uname", uname);
          profile.put("pw", pw);
          if (userDAO.signUp(uname, pw)) {
            storage.initProfile(profile);
            startActivity(intent);
            finish();
          } else {
            Notice.alert(context, "错误!", "注册失败，用户名已存在，请重新输入");
          }
        } else {
          Notice.alert(context, "错误!", "两次输入的密码不相同哦，请重新输入");
        }
      } else {
        Notice.alert(context, "提示", "密码不能为空");
        pwEditText.setText("");
        pwRepeatEditText.setText("");
      }
    } else {
      Notice.alert(context, "提示!", "请设置用户名");
    }
  }
}
